<?php
/**
 * Minhlog 内容详细页面功能控制器
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   Controller
 * @package    Frontend
 * @copyright  Copyright (c) 2009-2010 Minh.Shu@gmail.com
 * @license    New BSD License
 * @author     Minh.Shu
 * @version    $Id: DetailController.php 26 2010-02-25 15:49:05Z minh.shu $
 *
 */

class DetailController extends Minh_Controller_Action {
    protected $_articleModel;
    protected $_commentModel;
    protected $_pageModel;

    public function init() {
        if(null === $this->_articleModel) {
            if(class_exists('Domain_Model_Article')) {
                $this->_articleModel = new Domain_Model_Article();
            } else {
                throw new Exception('文章模型类不存在!');
            }
        }

        if(null === $this->_commentModel) {
            if(class_exists('Domain_Model_Comment')) {
                $this->_commentModel = new Domain_Model_Comment();
            } else {
                throw new Exception('文章评论模型类不存在!');
            }
        }

        if(null === $this->_pageModel) {
            if(class_exists('Domain_Model_Page')) {
                $this->_pageModel = new Domain_Model_Page();
            } else {
                throw new Exception('页面模型类不存在!');
            }
        }

        parent::init();
    }

    public function articleAction () {
        $filter = new Zend_Filter_Int();
        $articleId = $filter->filter($this->_getParam('id', 0));
        $page = $filter->filter($this->_getParam('page', 1));

        $articleCacheId = md5($this->_request->getControllerName() . '_' . $this->_request->getActionName() . '_' . $articleId);

        if(! ($articleCacheValue = $this->cache->load($articleCacheId))) {
            $article = $this->_articleModel->getArticleById($articleId);
            if(null === $article->info) {
                $this->_redirect('/index.html');
            }else {
                $article->info->articleSummary = $article->info->articleSummary;
                $article->info->articleContent = $article->info->articleContent;
                $article->info->publishTime = date('Y-m-d H:i:s', $article->info->publishTime);
                $this->cache->save($article, $articleCacheId);
            }
        } else {
            $article = $articleCacheValue;
        }

        $commentCacheId = md5($this->_request->getControllerName() . '_' . $this->_request->getActionName() . '_' . $articleId . '_comment' . '_' . $page);

        if(! ($commentCacheValue = $this->cache->load($commentCacheId))) {
            $comments = $this->_commentModel->getCommentByArticle($articleId);
            if(null !== $comments) {
                foreach ($comments as $comment) {
                    $comment->comment = $comment->comment;
                    $comment->replyTime = date('Y-m-d H:i:s',$comment->replyTime);
                }
                $perPageNum = 5;
                $comments = Zend_Paginator::factory($comments);
                $comments->setCurrentPageNumber($page)->setItemCountPerPage($perPageNum);
            }
            $this->cache->save($comments, $commentCacheId);
        } else {
            $comments = $commentCacheValue;
        }

        //SEO 信息
        if($page > 1) $this->view->headTitle()->append('Part '.$page);
        $this->view->headTitle($article->info->articleTitle, 'PREPEND');
        $this->view->headMeta()->appendName('keywords', $this->seo['detail']['article']['keywords'] . ',' . $article->info->articleTitle);
        $this->view->headMeta()->appendName('description',trim(strip_tags($article->info->articleSummary)));

        $this->view->article = $article;
        $this->view->comments = $comments;
        $this->view->page = $page;

        $this->fetchExecuteTime();

        echo $this->view->render('article.phtml');
    }

    public function pageAction () {
        $filter = new Zend_Filter_Int();
        $pageId = $filter->filter($this->_getParam('id', 0));

        $pageCacheId = md5($this->_request->getControllerName() . '_' . $this->_request->getActionName() . '_' . $pageId);

        if(! ($pageCacheValue = $this->cache->load($pageCacheId))) {
            $pageInfo = $this->_pageModel->getPageById($pageId);

            if(null === $pageInfo) {
                $this->_redirect('/index.html');
            }

            $pageInfo->pageContent = $pageInfo->pageContent;

            $this->cache->save($pageInfo, $pageCacheId);
        } else {
            $pageInfo = $pageCacheValue;
        }

        //SEO 信息
        $this->view->headTitle($pageInfo->pageTitle, 'PREPEND');
        $this->view->headMeta()->appendName('keywords', $this->seo['detail']['page']['keywords'] . ',' . $pageInfo->pageTitle);
        $this->view->headMeta()->appendName('description', trim(strip_tags($this->seo['detail']['page']['description'])));

        $this->view->pageInfo = $pageInfo;

        $this->fetchExecuteTime();

        echo $this->view->render('page.phtml');
    }
}